<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

include_once("sys/interfaces.inc");

class ScoreItemAction extends ItemAction implements iAction {
	public function execute($page) {
		require_once('sys/ScoreBoard.inc');
		$scoreboard = new Scoreboard($PAGE);
		$PAGE = $scoreboard->show($type);
	}
}

class MarkReadItemAction extends ItemAction implements iAction {
	public function execute($page) {
		if (!$_SESSION['user_id'])
			errorpage(6);
		$sql = db_query_insert_multiple(array('user_id', 'item_id'), array(array($_SESSION['user_id'], $page->id)), "HAS_READ", 1);
		db_do_query($page, $sql);
		add_alert("item #{$page->id} gelezen");
		$page->menu[$THISDIR['dir_id']]['new_items']--;
		$page->menu[$THISDIR['dir_id']]['read_items']++;
	}
}

class MarkUnreadItemAction extends ItemAction implements iAction {
	public function execute($page) {
		if (!$_SESSION['user_id'])
			errorpage(6);
		$sql = db_query_delete("HAS_READ", "item_id = {$page->id} AND user_id = {$_SESSION[user_id]}");
		$sqlresult = db_do_query($page, $sql);
		add_alert("item #{$page->id} ongelezen gemarkeerd");
		$page->menu[$THISDIR['dir_id']]['new_items']++;
		$page->menu[$THISDIR['dir_id']]['read_items']--;
	}
}

class StickItemAction extends ItemAction implements iAction {
	public function execute($page) {
		if (!$_SESSION['user_id'])
			errorpage(6);
		if (!has_access("MODERATE") and !has_access("ADMIN"))
			errorpage(5);
		$sql = db_query_update(array('sticky', 'creation', 'edit'), array(($action=="stick" ? 1 : 0), 'creation', 'edit'), "ITEM", "item_id = {$page->id}");
		db_do_query($page, $sql);
		add_alert("item #{$page->id} is nu " . ($action=="stick" ? "" : "niet meer ") . "sticky");
	}
}

class UnstickItemAction extends ItemAction implements iAction {
	public function execute($page) {
		if (!$_SESSION['user_id'])
			errorpage(6);
		if (!has_access("MODERATE") and !has_access("ADMIN"))
			errorpage(5);
		$sql = db_query_update(array('sticky', 'creation', 'edit'), array(($action=="stick" ? 1 : 0), 'creation', 'edit'), "ITEM", "item_id = {$page->id}");
		db_do_query($page, $sql);
		add_alert("item #{$page->id} is nu " . ($action=="stick" ? "" : "niet meer ") . "sticky");
	}
}

class NoItemAction extends ItemAction implements iAction {
	public function execute($page) {}
}

class OpenItemAction extends ItemAction implements iAction {
	public function execute($page) {
		if (!$_SESSION['user_id'])
			errorpage(6);
		if (!has_access("MODERATE") and !has_access("ADMIN"))
			errorpage(5);
		$sql = db_query_update(array('closed', 'creation', 'edit'), array(($action=="close" ? 1 : 0), 'creation', 'edit'), "ITEM", "item_id = {$page->id}");
		db_do_query($page, $sql);
		add_alert("item #{$page->id} is nu " . ($action=="open" ? "heropend" : "gesloten"));
	}
}

class CloseItemAction extends ItemAction implements iAction {
	public function execute($page) {
		if (!$_SESSION['user_id'])
			errorpage(6);
		if (!has_access("MODERATE") and !has_access("ADMIN"))
			errorpage(5);
		$sql = db_query_update(array('closed', 'creation', 'edit'), array(($action=="close" ? 1 : 0), 'creation', 'edit'), "ITEM", "item_id = {$page->id}");
		db_do_query($page, $sql);
		add_alert("item #{$page->id} is nu " . ($action=="open" ? "heropend" : "gesloten"));
	}
}

class UnknownItemAction extends ItemAction implements iAction {
	public function execute($page) {
		add_alert("Onbekende actie.");
	}
}

class ItemAction {
	public static function get_instance($action) {
		switch($action) {
			case "score":
				return new ScoreItemAction();
			case "markread":
				return new MarkReadItemAction();
			case "markunread":
				return new MarkUnreadItemAction();
			case "stick":
				return new StickItemAction();
			case "unstick":
				return new UnstickItemAction();
			case "edit_reply":
				return new NoItemAction();
			case "close":
				return new CloseItemAction();
			case "open":
				return new OpenItemAction();
			default:
				return new UnknownItemAction();
		}
	}
}
?>
